package com.yazi.mxz.fieldsale.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;




/**
 * 数据同步
 * @author mo_yq5
 * @date 2015-6-20
 */
@Controller("httpClientController")
@RequestMapping("/client")
public class CustomHttpClientController extends BaseController {
	
	
	@RequestMapping("/searchResult2.aspx")
	public ResponseEntity<String> def(HttpEntity<byte[]> requestEntity, String keyword, String __VIEWSTATE, String __EVENTTARGET, String __EVENTARGUMENT, String __EVENTVALIDATION) {
		CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
		
		httpclient.start();
		final HttpPost req = new HttpPost("http://search.anccnet.com/searchResult2.aspx");
		req.setHeader("User-Agent", requestEntity.getHeaders().getFirst("User-Agent"));
		req.setHeader("Referer", "http://search.anccnet.com/searchResult2.aspx");
		req.setHeader("Host", "search.anccnet.com");
		//req.setHeader("Connection", requestEntity.getHeaders().getFirst("Connection"));
		//req.setHeader("Accept-Language", requestEntity.getHeaders().getFirst("Accept-Language"));
		//req.setHeader("Accept-Encoding", requestEntity.getHeaders().getFirst("Accept-Encoding"));
		//req.setHeader("Accept", requestEntity.getHeaders().getFirst("Accept"));
		req.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=gb2312");
		
		 List<NameValuePair> params = new ArrayList<NameValuePair>();
	     params.add(new BasicNameValuePair("keyword", keyword));
	     params.add(new BasicNameValuePair("__VIEWSTATE", __VIEWSTATE));
	     params.add(new BasicNameValuePair("__EVENTTARGET", __EVENTTARGET));
	     params.add(new BasicNameValuePair("__EVENTARGUMENT", __EVENTARGUMENT));
	     params.add(new BasicNameValuePair("__EVENTVALIDATION", __EVENTVALIDATION));
	     
		// and wait until a response is received
		HttpResponse response1 = null;
		String result = "";
		try {
			req.setEntity(new UrlEncodedFormEntity(params,"gb2312"));
			Future<HttpResponse> future = httpclient.execute(req, null);
			response1 = future.get();
			System.out.println(req.getRequestLine() + "->" + response1.getStatusLine());
			result = EntityUtils.toString(response1.getEntity(), "gb2312");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpHeaders responseHeaders = new HttpHeaders();
	    responseHeaders.set("Content-Type", "text/html; charset=gb2312");
	    return new ResponseEntity<String>(result, responseHeaders, HttpStatus.CREATED);
	}
	
}
